// 引入koa框架
const Koa = require('koa');
// 实例化koa
const app = new Koa();
// 引入路由 middleware
const { loginRouter } = require('./router/login')
const { test } = require('./router/test')
const { fileRouter } = require('./router/file')
const { groupRouter } = require('./router/group')
const { personalRouter } = require('./router/personal')
// 引入地址权限校验 middleware
const  { errorHandle, validate } = require('./middleware/koa-jwt')
// 引入请求解析 middleware
const koaBody = require('./middleware/koa-body')
// 允许访问静态资源
const koaStatic = require('koa-static')

// 端口号
const PORT = 2333;


let staticPath = './public';
//在浏览器输入 localhost:2222/zz.jpg 
//                    监听的端口号 / 你指定的目录下的文件
app.use(koaStatic(staticPath));

// 引入跨域模块
const koaCors = require('koa-cors');
app.use(koaCors());

// 地址登录校验
// app.use(errorHandle).use(validate);

// 解析请求
app.use(koaBody);

// 注册路由
app
  .use(loginRouter.routes())
  .use(loginRouter.allowedMethods())

app
  .use(test.routes())
  .use(test.allowedMethods())

app
  .use(fileRouter.routes())
  .use(fileRouter.allowedMethods())

app
  .use(groupRouter.routes())
  .use(groupRouter.allowedMethods())

app
  .use(personalRouter.routes())
  .use(personalRouter.allowedMethods())

// 侦听
app.listen(PORT,()=>{
    console.log(`http://localhost:${PORT}`)
})